package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.dao.ProblemMapper;
import com.lucher.mes.dao.ProductMapper;
import com.lucher.mes.dao.ProductworkorderMapper;
import com.lucher.mes.dao.WorkorderMapper;
import com.lucher.mes.dao.WorkorderproblemMapper;
import com.lucher.mes.po.Problem;
import com.lucher.mes.po.Product;
import com.lucher.mes.po.Productworkorder;
import com.lucher.mes.po.Workline;
import com.lucher.mes.po.Worklinesection;
import com.lucher.mes.po.Worklinesectionmachine;
import com.lucher.mes.po.Workmachine;
import com.lucher.mes.po.Workorder;
import com.lucher.mes.po.Workorderproblem;
import com.lucher.mes.po.Worksection;

@Controller
@RequestMapping("/product")  
public class ProductController extends BaseController{
	@Autowired
	private ProductMapper productDao;
	@Autowired
	private ProductworkorderMapper productworkorderDao;
	@Autowired
	private WorkorderMapper workorderDao;
	@Autowired
	private ProblemMapper problemDao;
	@Autowired
	private WorkorderproblemMapper workorderproblemDao;
	
	/**
	 * 查询信息
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Product form) {
		return "product/findProductList";
	}
	
	/**
	 * 查询信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Product form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = productDao.findByCount(form);
		List<Product> list = productDao.findByListForPage(form);
		
		for (Product product : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			
			objectMap.put("id", product.getId());
			objectMap.put("productcode", product.getProductcode());
			objectMap.put("productname", product.getProductname());
			objectMap.put("productunit", product.getProductunit());
			objectMap.put("depotamount", product.getDepotamount());
			objectMap.put("remark", product.getRemark());
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}

	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addInit")
	public String addInit(Product form,Model model) {
		//查询所有的工序信息
		//List<Workorder> workorderlist = workorderDao.findByList(new Workorder());
		//model.addAttribute("workorderlist", workorderlist);
		return "product/addProduct";
	}

	/**
	 * 新增
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/save")
	public Map<String,Object>  save(Product form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getProductcode())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入产品编码");
			return responseJson;
		} else {
			Product oldProduct = productDao.findByProductcode(form.getProductcode());
			if (oldProduct != null) {
				responseJson.put("flag", "0");//操作失败
				responseJson.put("msg", "操作失败，此产品编码已经存在");
				return responseJson;
			}
		}
		
		if ("".equals(form.getProductname())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入产品名称");
			return responseJson;
		} else {
			Product product = productDao.findByProductname(form.getProductname());
			if (product != null) {
				responseJson.put("flag", "0");//操作失败
				responseJson.put("msg", "操作失败，此产品名称已经存在");
				return responseJson;
			}
		}
		
//		String ordercodes = form.getOrdercodes();
//		if(StringUtils.isBlank(ordercodes)){
//			responseJson.put("flag", "0");//操作失败
//			responseJson.put("msg", "操作失败，请先选择工序信息");
//			return responseJson;
//		}
		
		//保存材料信息
		Integer productId = productDao.insertSelective(form);
		
		//新添加关联的工序
//		String[] ordercodeArr = ordercodes.split(",");
//		for (String ordercode : ordercodeArr) {
//			Workorder workorder = workorderDao.findByOrdercode(ordercode);
//			Productworkorder productworkorder = new Productworkorder();
//			productworkorder.setProductcode(form.getProductcode());
//			productworkorder.setProductname(form.getProductname());
//			productworkorder.setOrdercode(ordercode);
//			productworkorder.setOrdername(workorder.getOrdername());
//			productworkorderDao.insertSelective(productworkorder);
//		}
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}

	/**
	 * 更新初始化
	 */
	@RequestMapping(value = "/updateInit")
	public String updateInit(Product form,Model model) {
		Product product = productDao.selectByPrimaryKey(form.getId());
		form.setProduct(product);

		//查询所有已经被选用的
//		Productworkorder record = new Productworkorder();
//		record.setProductcode(product.getProductcode());
//		List<Productworkorder> productworkorderList = productworkorderDao.findByList(record);
//		
//		//查询所有的工位机信息
//		List<Workorder> workorderlist = workorderDao.findByList(new Workorder());
//		
//		//循环所有的工位机信息
//		for (Workorder workorder : workorderlist) {
//			for (Productworkorder productworkorder : productworkorderList) {
//				if(workorder.getOrdercode().equals(productworkorder.getOrdercode())){
//					workorder.setPageSelected(true);
//				}else{
//					continue;
//				}
//			}
//		}
//		
//		model.addAttribute("workorderlist", workorderlist);
		
		return "product/updateProduct";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/update")
	@Transactional
	public Map<String,Object> update(Product form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		Product product_old = productDao.selectByPrimaryKey(form.getId());
		
		if ("".equals(form.getProductcode())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，产品编码不能为空");
			return responseJson;
		} else {
			Product oldProduct = productDao.findByProductcode(form.getProductcode());
			if (oldProduct != null && !oldProduct.getId().equals(form.getId())) {
				responseJson.put("flag", "0"); //添加失败
				responseJson.put("msg", "操作失败，产品编码已经存在");
				return responseJson;
			}
		}
		
		if ("".equals(form.getProductname())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，产品名称不能为空");
			return responseJson;
		} else {
			Product oldProduct = productDao.findByProductname(form.getProductname());
			if (oldProduct != null && !oldProduct.getId().equals(form.getId())) {
				responseJson.put("flag", "0"); //添加失败
				responseJson.put("msg", "操作失败，产品名称已经存在");
				return responseJson;
			}
		}

//		String ordercodes = form.getOrdercodes();
//		if(StringUtils.isBlank(ordercodes)){
//			responseJson.put("flag", "0");//操作失败
//			responseJson.put("msg", "操作失败，请先选择工序信息");
//			return responseJson;
//		}
		
		// 修改信息
		int flag = productDao.updateByPrimaryKeySelective(form);
		
//		//删除以前的工位机
//		Productworkorder wroductworkorderDemo = new Productworkorder();
//		wroductworkorderDemo.setProductcode(product_old.getProductcode());
//		productworkorderDao.deleteByProductcode(wroductworkorderDemo);
//		
//		//新添加关联的工序
//		String[] ordercodeArr = ordercodes.split(",");
//		for (String ordercode : ordercodeArr) {
//			Workorder workorder = workorderDao.findByOrdercode(ordercode);
//			Productworkorder productworkorder = new Productworkorder();
//			productworkorder.setProductcode(form.getProductcode());
//			productworkorder.setProductname(form.getProductname());
//			productworkorder.setOrdercode(ordercode);
//			productworkorder.setOrdername(workorder.getOrdername());
//			productworkorderDao.insertSelective(productworkorder);
//		}
		
		//查询所有的工序信息,修改信息
		Productworkorder record = new Productworkorder();
		record.setProductcode(product_old.getProductcode());
		List<Productworkorder> productworkorderList = productworkorderDao.findByList(record);
		for (Productworkorder productworkorder : productworkorderList) {
			productworkorder.setProductcode(form.getProductcode());
			productworkorder.setProductname(form.getProductname());
			productworkorderDao.updateByPrimaryKeySelective(productworkorder);
		}
		
		responseJson.put("flag", "1");//
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
    
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/delete")
	public Map<String,Object> delete(Product form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		//删除产品
		productDao.deleteByPrimaryKey(form.getId());
		
		//删除工序本身
		Workorder record = new Workorder();
		record.setProductcode(form.getProductcode());;
		List<Workorder> workorderList = workorderDao.findByList(record);
		for (Workorder workorder : workorderList) {
			//删除工序对应的问题
			workorderproblemDao.deleteByOrdercode(workorder.getOrdercode());
			workorderDao.deleteByPrimaryKey(workorder.getId());
		}
		
		//删除关联的工序
		Productworkorder record1 = new Productworkorder();
		record1.setProductcode(form.getProductcode());
		productworkorderDao.deleteByProductcode(record1);
		
		
		
		
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/deleteBatch")
	public Map<String,Object> deleteBatch(@RequestBody List<Product> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		for (Product product : formlist) {
			//删除材料
			productDao.deleteByPrimaryKey(product.getId());
			
			//删除关联的工序
			Productworkorder record1 = new Productworkorder();
			record1.setProductcode(product.getProductcode());
			productworkorderDao.deleteByProductcode(record1);
		}

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
     * 获取下拉列表框Json值
     */
	@ResponseBody //此标签表示返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	@RequestMapping(value="/getProductCombogridJson")
	public List<Map<String,Object>> getProductCombogridJson(Product form) {  
	    List<Product> productList = productDao.findByList(form);
	    List<Map<String, Object>> productCombogridJson = new ArrayList<Map<String, Object>>();
	    //封装属性结构信息
	    for(Product product:productList){  
	    	    HashMap<String,Object> productMap = new HashMap<String, Object>();
	    	    productMap.put("id", product.getId());
	    	    productMap.put("productcode", product.getProductcode());
	    	    productMap.put("productname", product.getProductname());
	    	    productMap.put("productsource", product.getProductsource());
	    	    productMap.put("depotamount", product.getDepotamount());
	    	    productMap.put("productunit", product.getProductunit());
	    	    productCombogridJson.add(productMap);  
	    }  
	   return productCombogridJson;
	}  
	
	/**
	 * 更新产品关联工序初始化
	 */
	@RequestMapping(value = "/updateworkorderInit")
	public String updateworkorderInit(Product form,Model model) {
		form = productDao.selectByPrimaryKey(form.getId());
		model.addAttribute("product", form);
		
		//查询所有已经被选用的
		Productworkorder record = new Productworkorder();
		record.setProductcode(form.getProductcode());
		List<Productworkorder> productworkorderList = productworkorderDao.findByList(record);
		
		//查询所有的工位机信息
		List<Workorder> workorderlist = workorderDao.findByList(new Workorder());
		
		//循环所有的工位机信息
		for (Workorder workorder : workorderlist) {
			for (Productworkorder productworkorder : productworkorderList) {
				if(workorder.getOrdercode().equals(productworkorder.getOrdercode())){
					workorder.setPageSelected(true);
				}else{
					continue;
				}
			}
		}
		
		model.addAttribute("workorderlist", workorderlist);

		return "product/updateWorkorder";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/updateworkorder")
	public Map<String,Object> updateworkorder(Product form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");

		String ordercodes = form.getOrdercodes();
		if(StringUtils.isBlank(ordercodes)){
			responseJson.put("flag", "0");//操作失败
			responseJson.put("msg", "操作失败，请先选择工序信息");
			return responseJson;
		}
		
		//删除以前的工位机
		Productworkorder productworkorderDemo = new Productworkorder();
		productworkorderDemo.setProductcode(form.getProductcode());
		productworkorderDao.deleteByProductcode(productworkorderDemo);
		
		//新添加关联的工位机
		String[] ordercodeArr = ordercodes.split(",");
		for (String ordercode : ordercodeArr) {
			Workorder workorder = workorderDao.findByOrdercode(ordercode);
			Productworkorder productworkorder = new Productworkorder();
			productworkorder.setProductcode(form.getProductcode());
			productworkorder.setProductname(form.getProductname());
			productworkorder.setOrdercode(ordercode);
			productworkorder.setOrdername(workorder.getOrdername());
			productworkorderDao.insertSelective(productworkorder);
		}
		
		responseJson.put("flag", "1");//
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
	
	/**
	 * 查询产品关联工序信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findWorkorderListJson")
	public Map<String, Object> findWorkorderListJson(Productworkorder form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = productworkorderDao.findByCount(form);
		List<Productworkorder> list = productworkorderDao.findByListForPage(form);
		
		for (Productworkorder productworkorder : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			objectMap.put("productworkorderid", productworkorder.getId());
			objectMap.put("productcode", productworkorder.getProductcode());
			objectMap.put("productname", productworkorder.getProductname());
			objectMap.put("ordercode", productworkorder.getOrdercode());
			objectMap.put("ordername", productworkorder.getOrdername());
			objectMap.put("serialnumber", productworkorder.getSerialnumber());
			Workorder workorder = workorderDao.findByOrdercode(productworkorder.getOrdercode());
			if(workorder != null){
				objectMap.put("id", workorder.getId());
				objectMap.put("info", workorder.getInfo());
				objectMap.put("filepath", workorder.getFilepath());
			}
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}
	
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/updateOrdering")
	public Map<String,Object> updateOrdering(Productworkorder record) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		//修改关联的工位机
		record.setId(record.getProductworkorderid());
		productworkorderDao.updateByPrimaryKeySelective(record);
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
}
